function throttle(callback, wait) {
  let that, args, timeout;
  let oldTime = 0;
  return function() {
    that = this;
    args = arguments;
    let nowTime = Date.now();
    let later= function() {
      callback.apply(that, args);
      oldTime = Date.now();
      clearTimeout(timeout);
      timeout = null;
    }

    if( nowTime - oldTime > wait) {
      if(timeout) {
        clearTimeout(timeout);
        timeout = null;
      }
      callback.apply(that, args)
      oldTime = nowTime;
    } else if (!timeout) {
      timeout = setTimeout(later, wait)
    }
  }
}